# Copyright (c) 2024 Huawei Technologies Co., Ltd.
# openUBMC is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#         http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.

# -*- coding: utf-8 -*-
"""
测试列表方法的内建函数处理
"""

from tests.pytests.helpers.codegen_helpers import compile_chunk, assert_code_contains
from typing import Any


def test_list_insert_method():
    """测试 list.insert() 方法"""

    def test_insert_function(my_list: list[int]) -> None:
        """测试列表insert方法"""
        my_list.insert(1, 42)

    cpp = compile_chunk(test_insert_function)
    expected = """
    void test_insert_function(mc::array<int>& my_list) {
        mcpy::list_insert(my_list, 1, 42);
    }
    """
    assert_code_contains(cpp, expected)


def test_list_pop_method_no_args():
    """测试 list.pop() 方法（无参数）"""

    def test_pop_function(my_list: list[int]) -> int:
        """测试列表pop方法（无参数）"""
        return my_list.pop()

    cpp = compile_chunk(test_pop_function)
    expected = """
    int test_pop_function(mc::array<int>& my_list) {
        return mcpy::list_pop_back(my_list);
    }
    """
    assert_code_contains(cpp, expected)


def test_list_pop_method_with_index():
    """测试 list.pop(index) 方法"""

    def test_pop_index_function(my_list: list[int]) -> int:
        """测试列表pop方法（带索引）"""
        return my_list.pop(0)

    cpp = compile_chunk(test_pop_index_function)
    expected = """
    int test_pop_index_function(mc::array<int>& my_list) {
        return mcpy::list_pop(my_list, 0);
    }
    """
    assert_code_contains(cpp, expected)


def test_list_copy_method():
    """测试 list.copy() 方法"""

    def test_copy_function(my_list: list[int]) -> list[int]:
        """测试列表copy方法"""
        return my_list.copy()

    cpp = compile_chunk(test_copy_function)
    expected = """
    mc::array<int> test_copy_function(const mc::array<int>& my_list) {
        return my_list.copy();
    }
    """
    assert_code_contains(cpp, expected)


def test_list_index_method():
    """测试 list.index() 方法"""

    def test_index_function(my_list: list[int]) -> int:
        """测试列表index方法"""
        return my_list.index(42)

    cpp = compile_chunk(test_index_function)
    expected = """
    int test_index_function(const mc::array<int>& my_list) {
        return mcpy::list_index(my_list, 42);
    }
    """
    assert_code_contains(cpp, expected)


def test_list_count_method():
    """测试 list.count() 方法"""

    def test_count_function(my_list: list[int]) -> int:
        """测试列表count方法"""
        return my_list.count(42)

    cpp = compile_chunk(test_count_function)
    expected = """
    int test_count_function(const mc::array<int>& my_list) {
        return mcpy::list_count(my_list, 42);
    }
    """
    assert_code_contains(cpp, expected)


def test_existing_list_methods():
    """测试已有的列表方法仍然工作"""

    # 测试 append
    def test_append_function(my_list: list[int]) -> None:
        """测试列表append方法"""
        my_list.append(42)

        ll = []
        ll.append(42)

    cpp = compile_chunk(test_append_function)
    expected = """
    void test_append_function(mc::array<int>& my_list) {
        my_list.push_back(42);

        mc::variants ll = {};
        ll.push_back(42);
    }
    """
    assert_code_contains(cpp, expected)

    # 测试 extend
    def test_extend_function(my_list: list[int], other_list: list[int]) -> None:
        """测试列表extend方法"""
        my_list.extend(other_list)

    cpp = compile_chunk(test_extend_function)
    expected = """
    void test_extend_function(mc::array<int>& my_list, const mc::array<int>& other_list) {
        mcpy::list_extend(my_list, other_list);
    }
    """
    assert_code_contains(cpp, expected)

    # 测试 clear
    def test_clear_function(my_list: list[int]) -> None:
        """测试列表clear方法"""
        my_list.clear()

    cpp = compile_chunk(test_clear_function)
    expected = """
    void test_clear_function(mc::array<int>& my_list) {
        my_list.clear();
    }
    """
    assert_code_contains(cpp, expected)

    # 测试 reverse
    def test_reverse_function(my_list: list[int]) -> None:
        """测试列表reverse方法"""
        my_list.reverse()

    cpp = compile_chunk(test_reverse_function)
    expected = """
    void test_reverse_function(mc::array<int>& my_list) {
        mcpy::list_reverse(my_list);
    }
    """
    assert_code_contains(cpp, expected)

    # 测试 sort
    def test_sort_function(my_list: list[int]) -> None:
        """测试列表sort方法"""
        my_list.sort()

    cpp = compile_chunk(test_sort_function)
    expected = """
    void test_sort_function(mc::array<int>& my_list) {
        std::sort(my_list.begin(), my_list.end());
    }
    """
    assert_code_contains(cpp, expected)

    # 测试 remove
    def test_remove_function(my_list: list[int]) -> None:
        """测试列表remove方法"""
        my_list.remove(42)

    cpp = compile_chunk(test_remove_function)
    expected = """
    void test_remove_function(mc::array<int>& my_list) {
        mcpy::list_remove(my_list, 42);
    }
    """
    assert_code_contains(cpp, expected)


def test_list_append_operation():
    """测试列表append操作的编译"""

    def append_to_list(items: list[int], value: int) -> None:
        """向列表添加元素"""
        items.append(value)

    cpp = compile_chunk(append_to_list)
    expected = """
    void append_to_list(mc::array<int>& items, int value) {
        items.push_back(value);
    }
    """
    assert_code_contains(cpp, expected)


def test_list_extend_operation():
    """测试列表extend操作的编译"""

    def extend_list(items: list[int], other: list[int]) -> None:
        """将另一个列表的所有元素添加到当前列表"""
        items.extend(other)

    cpp = compile_chunk(extend_list)
    expected = """
    void extend_list(mc::array<int>& items, const mc::array<int>& other) {
        mcpy::list_extend(items, other);
    }
    """
    assert_code_contains(cpp, expected)


def test_list_remove_operation():
    """测试列表remove操作的编译"""

    def remove_from_list(items: list[int], value: int) -> None:
        """从列表中移除指定元素"""
        items.remove(value)

    cpp = compile_chunk(remove_from_list, "remove_from_list")
    expected = "mcpy::list_remove(items, value);"
    assert_code_contains(cpp, expected)


def test_list_empty_check():
    """测试列表空检查的编译"""

    def check_empty_list(items: list[int]) -> int:
        """检查列表是否为空"""
        if not items:
            return 0
        return 1

    cpp = compile_chunk(check_empty_list)
    expected = """
    int check_empty_list(const mc::array<int>& items) {
        if (items.empty()) {
            return 0;
        }
        return 1;
    }
    """
    assert_code_contains(cpp, expected)


def test_list_clear_operation():
    """测试列表清空操作的编译"""

    def clear_list(items: list[int]) -> None:
        """清空列表中的所有元素"""
        items.clear()

    cpp = compile_chunk(clear_list)
    expected = """
    void clear_list(mc::array<int>& items) {
        items.clear();
    }
    """
    assert_code_contains(cpp, expected)


def test_list_reverse_operation():
    """测试列表反转操作的编译"""

    def reverse_list(items: list[int]) -> None:
        """反转列表中元素的顺序"""
        items.reverse()

    cpp = compile_chunk(reverse_list)
    expected = """
    void reverse_list(mc::array<int>& items) {
        mcpy::list_reverse(items);
    }
    """
    assert_code_contains(cpp, expected)


def test_list_sort_operation():
    """测试列表排序操作的编译"""

    def sort_list(items: list[int]) -> None:
        """对列表中的元素进行排序"""
        items.sort()

    cpp = compile_chunk(sort_list)
    expected = """
    void sort_list(mc::array<int>& items) {
        std::sort(items.begin(), items.end());
    }
    """
    assert_code_contains(cpp, expected)


def test_list_comprehension_like_operations():
    """测试类似列表推导式的操作编译"""

    def filter_and_transform(numbers: list[int]) -> list[int]:
        """过滤和转换列表元素"""
        result = [1]
        result1: list[int] = [2]
        for num in numbers:
            if num > 0:
                result.append(num * 2)
                result1.append(num * 3)
        return result

    cpp = compile_chunk(filter_and_transform)
    assert_code_contains(
        cpp,
        """
    mc::array<int> filter_and_transform(const mc::array<int>& numbers) {
        mc::array<int> result = {1};
        mc::array<int> result1 = {2};
        for (const auto& num : numbers) {
            if (num > 0) {
                result.push_back(num * 2);
                result1.push_back(num * 3);
            }
        }
        return result;
    }
    """,
    )


def test_list_with_different_types():
    """测试不同类型列表操作的编译"""

    def process_string_list(strings: list[str]) -> str:
        """处理字符串列表"""
        if not strings:
            return ""
        return str(len(strings))

    cpp = compile_chunk(process_string_list)
    expected = """
    std::string process_string_list(const mc::array<std::string>& strings) {
        if (strings.empty()) {
            return "";
        }
        return mc::to_string(strings.size());
    }
    """
    assert_code_contains(cpp, expected)


def test_cpp_vector_type_recognition():
    """测试C++的std::vector类型能被正确识别为list"""

    # 测试 index 方法
    def test_vector_index_function(cpp_vec: list[float]) -> int:
        """测试C++向量的index方法"""
        return cpp_vec.index(1.0)

    cpp = compile_chunk(test_vector_index_function)
    expected = """
    int test_vector_index_function(const mc::array<double>& cpp_vec) {
        return mcpy::list_index(cpp_vec, 1.0);
    }
    """
    assert_code_contains(cpp, expected)

    # 测试 count 方法
    def test_vector_count_function(cpp_vec: list[float]) -> int:
        """测试C++向量的count方法"""
        return cpp_vec.count(1.0)

    cpp = compile_chunk(test_vector_count_function)
    expected = """
    int test_vector_count_function(const mc::array<double>& cpp_vec) {
        return mcpy::list_count(cpp_vec, 1.0);
    }
    """
    assert_code_contains(cpp, expected)


def test_variant_list():
    """测试 variant 列表"""

    def process_variant_list() -> int:
        numbers = [1, "str", True]
        numbers.append(0.2)
        empty_list_0: list[Any] = []
        empty_list_1: list = []
        return len(numbers)

    cpp = compile_chunk(process_variant_list)
    expected = """
    int process_variant_list() {
        mc::variants numbers = {1, "str", true};
        numbers.push_back(0.2);
        mc::variants empty_list_0 = {};
        mc::variants empty_list_1 = {};
        return numbers.size();
    }
    """
    assert_code_contains(cpp, expected)


def test_literal_list():
    """测试字面值列表类型推导"""
    code = """
def list_concat(l: list[int], r: list[int]) -> list[int]:
    tmp: list[int] = l
    tmp.extend(r)
    return tmp

def test_literal_list():
    numbers: list[int] = list_concat([1, 2], [3, 4])
    expected = [1, 2, 3, 4]
"""

    cpp = compile_chunk(code)
    assert_code_contains(
        cpp,
        """
        mc::array<int> list_concat(const mc::array<int>& l, const mc::array<int>& r) {
            mc::array<int> tmp = l;
            mcpy::list_extend(tmp, r);
            return tmp;
        }
        void test_literal_list() {
            mc::array<int> numbers  = list_concat({1, 2}, {3, 4});
            mc::array<int> expected = {1, 2, 3, 4};
        }
        """,
    )


def test_list_constructor_empty():
    """测试 list() 空构造函数"""

    def create_empty_list() -> list[int]:
        """创建空列表"""
        return list()

    cpp = compile_chunk(create_empty_list)
    expected = """
    mc::array<int> create_empty_list() {
        return mc::array<int>{};
    }
    """
    assert_code_contains(cpp, expected)


def test_weak_list_constructor():
    """测试弱类型 list() 构造函数"""

    def create_weak_list():
        v: list = list([1, 2])
        v.extend([3, 4])
        v.extend(list[int]([5, 6]))
        v.extend(list[float]({7, 8}))
        v.extend(list([9, 10]))
        return v

    cpp = compile_chunk(create_weak_list)
    expected = """
    mc::variants create_weak_list() {
        mc::variants v = mcpy::list_construct<mc::variant>(mc::variants{1, 2});
        mcpy::list_extend(v, {3, 4});
        mcpy::list_extend(v, mcpy::list_construct<int>(mc::array<int>{5, 6}));
        mcpy::list_extend(v, mcpy::list_construct<double>(mcpy::variant_set{7, 8}));
        mcpy::list_extend(v, mcpy::list_construct<mc::variant>(mc::variants{9, 10}));
        return v;
    }
    """
    assert_code_contains(cpp, expected)


def test_list_constructor_from_list():
    """测试 list([...]) 从列表构造"""

    def create_from_list(source: list[int]) -> list[int]:
        """从另一个列表构造新列表"""
        return list(source)

    cpp = compile_chunk(create_from_list)
    expected = """
    mc::array<int> create_from_list(const mc::array<int>& source) {
        return mcpy::list_construct<int>(source);
    }
    """
    assert_code_contains(cpp, expected)


def test_list_constructor_from_literal():
    """测试 list([1, 2]) 从字面量列表构造"""

    def create_from_literal() -> list[int]:
        """从字面量列表构造"""
        return list([1, 2, 3])

    cpp = compile_chunk(create_from_literal)
    expected = """
    mc::array<int> create_from_literal() {
        return mcpy::list_construct<int>(mc::array<int>{1, 2, 3});
    }
    """
    assert_code_contains(cpp, expected)


def test_list_constructor_in_iadd():
    """测试 list([...]) 在 += 操作中的使用"""

    def add_list_from_constructor(numbers: list[int]) -> None:
        """使用 list() 构造并添加到列表"""
        numbers += list([8, 9])

    cpp = compile_chunk(add_list_from_constructor)
    expected = """
    void add_list_from_constructor(mc::array<int>& numbers) {
        mcpy::list_iadd(numbers, mcpy::list_construct<int>(mc::array<int>{8, 9}));
    }
    """
    assert_code_contains(cpp, expected)


def test_list_constructor_in_extend():
    """测试 list([...]) 在 extend 方法中的使用"""

    def extend_list_from_constructor(numbers: list[int]) -> None:
        """使用 list() 构造并扩展到列表"""
        numbers.extend(list([10, 11]))

    cpp = compile_chunk(extend_list_from_constructor)
    expected = """
    void extend_list_from_constructor(mc::array<int>& numbers) {
        mcpy::list_extend(numbers, mcpy::list_construct<int>(mc::array<int>{10, 11}));
    }
    """
    assert_code_contains(cpp, expected)


def test_list_constructor_from_variable():
    """测试从变量构造列表"""

    def create_from_variable() -> list[int]:
        """从变量构造列表"""
        source = [1, 2, 3]
        return list(source)

    cpp = compile_chunk(create_from_variable)
    expected = """
    mc::array<int> create_from_variable() {
        mc::array<int> source = {1, 2, 3};
        return mcpy::list_construct<int>(source);
    }
    """
    assert_code_contains(cpp, expected)


def test_list_constructor_with_different_types():
    """测试不同类型列表的构造"""

    def create_string_list() -> list[str]:
        """创建字符串列表"""
        return list(["a", "b", "c"])

    cpp = compile_chunk(create_string_list)
    expected = """
    mc::array<std::string> create_string_list() {
        return mcpy::list_construct<std::string>(mc::array<std::string>{"a", "b", "c"});
    }
    """
    assert_code_contains(cpp, expected)


def test_list_addition_operator():
    """测试列表加法运算符重载 (__add__)"""

    def concat_lists(a: list[int], b: list[int]) -> list[int]:
        """使用 + 运算符连接两个列表"""
        return a + b

    cpp = compile_chunk(concat_lists)
    expected = """
    mc::array<int> concat_lists(const mc::array<int>& a, const mc::array<int>& b) {
        return mcpy::list_concat(a, b);
    }
    """
    assert_code_contains(cpp, expected)


def test_list_equality_operators():
    """测试列表相等和不等运算符重载 (__eq__ 和 __ne__)"""

    def compare_lists(a: list[int], b: list[int]) -> int:
        """比较两个列表是否相等"""
        result = 0
        if a == b:
            result = result + 1
        if a != b:
            result = result + 2
        return result

    cpp = compile_chunk(compare_lists)
    expected = """
    int compare_lists(const mc::array<int>& a, const mc::array<int>& b) {
        int result = 0;
        if (a == b) {
            result = result + 1;
        }
        if (a != b) {
            result = result + 2;
        }
        return result;
    }
    """
    assert_code_contains(cpp, expected)


def test_list_bool_operator_if():
    """测试列表布尔运算符重载在 if 语句中的使用

    'if items:' 会触发隐式布尔转换，调用 __bool__ 方法生成 !items.empty()
    """

    def check_list_if(items: list[int]) -> int:
        """使用 if items 检查列表"""
        if items:
            return 1
        return 0

    cpp = compile_chunk(check_list_if)
    expected = """
    int check_list_if(const mc::array<int>& items) {
        if (!items.empty()) {
            return 1;
        }
        return 0;
    }
    """
    assert_code_contains(cpp, expected)


def test_list_combined_operators():
    """测试列表运算符的组合使用"""

    def process_lists(a: list[int], b: list[int], c: list[int]) -> list[int]:
        """组合使用多个列表运算符"""
        # 使用 + 连接列表
        combined = a + b

        # 使用 == 比较
        if combined == c:
            return combined

        # 使用 != 和 not
        if combined != c and not combined:
            return []

        return combined + c

    cpp = compile_chunk(process_lists)
    expected = """
    mc::array<int> process_lists(const mc::array<int>& a, const mc::array<int>& b, const mc::array<int>& c) {
        mc::array<int> combined = mcpy::list_concat(a, b);
        if (combined == c) {
            return combined;
        }
        if (combined != c && combined.empty()) {
            return mc::array<int>{};
        }
        return mcpy::list_concat(combined, c);
    }
    """
    assert_code_contains(cpp, expected)


def test_reversed_list_lvalue():
    """测试 reversed(list) 的左值引用版本"""

    code = """
def test_reversed_lvalue(items: list[int]) -> None:
    '''测试 reversed 左值版本'''
    for item in reversed(items):
        pass
"""

    cpp = compile_chunk(code)
    expected = """
    void test_reversed_lvalue(const mc::array<int>& items) {
        for (const auto& item : mcpy::iterable_reversed(items)) {
        }
    }
    """
    assert_code_contains(cpp, expected)


def test_reversed_list_rvalue():
    """测试 reversed(list) 的右值版本（字面量）"""

    code = """
def test_reversed_rvalue() -> None:
    '''测试 reversed 右值版本 - 验证临时对象生命周期'''
    for item in reversed([1, 2, 3]):
        pass
"""

    cpp = compile_chunk(code)
    # 列表字面量被转换为 mc::array<int>{...} 构造
    expected = """
    void test_reversed_rvalue() {
        for (const auto& item : mcpy::iterable_reversed(mc::array<int>{1, 2, 3})) {
        }
    }
    """
    assert_code_contains(cpp, expected)


def test_zip_function_lvalue():
    """测试 zip() 函数的左值版本"""

    code = """
def test_zip_lvalue(a: list[int], b: list[str]) -> None:
    '''测试 zip 左值版本'''
    for pair in zip(a, b):
        pass
"""

    cpp = compile_chunk(code)
    expected = """
    void test_zip_lvalue(const mc::array<int>& a, const mc::array<std::string>& b) {
        for (const auto& pair : mcpy::zip_view(a, b)) {
        }
    }
    """
    assert_code_contains(cpp, expected)


def test_zip_function_rvalue():
    """测试 zip() 函数的右值版本（字面量）"""

    code = """
def test_zip_rvalue() -> None:
    '''测试 zip 右值版本 - 验证临时对象生命周期'''
    for pair in zip([1, 2, 3], ["a", "b", "c"]):
        pass
"""

    cpp = compile_chunk(code)
    # 列表字面量被转换为 mc::array 构造
    expected = """
    void test_zip_rvalue() {
        for (const auto& pair : mcpy::zip_view(mc::array<int>{1, 2, 3}, mc::array<std::string>{"a", "b", "c"})) {
        }
    }
    """
    assert_code_contains(cpp, expected)


def test_enumerate_function_lvalue():
    """测试 enumerate() 函数的左值版本"""

    code = """
def test_enumerate_lvalue(items: list[str]) -> None:
    '''测试 enumerate 左值版本'''
    for pair in enumerate(items):
        pass
"""

    cpp = compile_chunk(code)
    expected = """
    void test_enumerate_lvalue(const mc::array<std::string>& items) {
        for (const auto& pair : mcpy::enumerate(items)) {
        }
    }
    """
    assert_code_contains(cpp, expected)


def test_enumerate_function_with_start():
    """测试 enumerate() 带 start 参数"""

    code = """
def test_enumerate_start(items: list[int]) -> None:
    '''测试 enumerate 带 start 参数'''
    for pair in enumerate(items, start=10):
        pass
"""

    cpp = compile_chunk(code)
    expected = """
    void test_enumerate_start(const mc::array<int>& items) {
        for (const auto& pair : mcpy::enumerate(items, 10)) {
        }
    }
    """
    assert_code_contains(cpp, expected)


def test_enumerate_function_rvalue():
    """测试 enumerate() 的右值版本（字面量）"""

    code = """
def test_enumerate_rvalue() -> None:
    '''测试 enumerate 右值版本 - 验证临时对象生命周期'''
    for pair in enumerate([10, 20, 30], start=1):
        pass
"""

    cpp = compile_chunk(code)
    # 列表字面量被转换为 mc::array 构造
    expected = """
    void test_enumerate_rvalue() {
        for (const auto& pair : mcpy::enumerate(mc::array<int>{10, 20, 30}, 1)) {
        }
    }
    """
    assert_code_contains(cpp, expected)


def test_sorted_function():
    """测试 sorted() 全局函数的代码生成"""

    code = """
def test_sorted(items: list[int]) -> list[int]:
    '''测试 sorted 函数'''
    return sorted(items)
"""

    cpp = compile_chunk(code)
    expected = """
    mc::array<int> test_sorted(const mc::array<int>& items) {
        return mcpy::iterable_sorted<int>(items);
    }
    """
    assert_code_contains(cpp, expected)


def test_sorted_with_reverse():
    """测试 sorted() 带 reverse 参数"""

    code = """
def test_sorted_reverse(items: list[int]) -> list[int]:
    '''测试 sorted 带 reverse 参数'''
    return sorted(items, reverse=True)
"""

    cpp = compile_chunk(code)
    expected = """
    mc::array<int> test_sorted_reverse(const mc::array<int>& items) {
        return mcpy::iterable_sorted<int>(items, true);
    }
    """
    assert_code_contains(cpp, expected)


def test_sum_function():
    """测试 sum() 全局函数的代码生成"""

    code = """
def test_sum(numbers: list[int]) -> int:
    '''测试 sum 函数'''
    return sum(numbers)
"""

    cpp = compile_chunk(code)
    # sum 现在使用通用的 reduce_sum
    assert "mcpy::reduce_sum" in cpp


def test_min_max_functions():
    """测试 min() 和 max() 全局函数的代码生成"""

    code = """
def test_min_max(numbers: list[int]) -> tuple[int, int]:
    '''测试 min 和 max 函数'''
    min_val: int = min(numbers)
    max_val: int = max(numbers)
    return (min_val, max_val)
"""

    cpp = compile_chunk(code)
    expected = """
    std::tuple<int, int> test_min_max(const mc::array<int>& numbers) {
        int min_val = mcpy::iterable_min<int>(numbers);
        int max_val = mcpy::iterable_max<int>(numbers);
        return std::make_tuple(min_val, max_val);
    }
    """
    assert_code_contains(cpp, expected)


def test_any_all_functions():
    """测试 any() 和 all() 全局函数的代码生成"""

    code = """
def test_any_all(flags: list[bool]) -> tuple[bool, bool]:
    '''测试 any 和 all 函数'''
    any_result: bool = any(flags)
    all_result: bool = all(flags)
    return (any_result, all_result)
"""

    cpp = compile_chunk(code)
    expected = """
    std::tuple<bool, bool> test_any_all(const mc::array<bool>& flags) {
        bool any_result = mcpy::iterable_any(flags);
        bool all_result = mcpy::iterable_all(flags);
        return std::make_tuple(any_result, all_result);
    }
    """
    assert_code_contains(cpp, expected)


def test_list_slice():
    def test_list_slice():
        list1 = [1, 2, 3]
        list2 = list1[1:2]

        list3: list = [1, 2, 3]
        list4 = list3[1:2]

    cpp = compile_chunk(test_list_slice)
    expected = """
    void test_list_slice() {
        mc::array<int> list1 = {1, 2, 3};
        mc::array<int> list2 = mcpy::list_slice(list1, 1, 2, 1);
        mc::variants list3 = {1, 2, 3};
        mc::variants list4 = mcpy::list_slice(list3, 1, 2, 1);
    }
    """
    assert_code_contains(cpp, expected)
